<template>
  <!-- 侧边栏 -->
  <div class="sidebar">
    <div class="rightbar" v-if="btnFlag">
      <ul class="sidebar_ul">
        <li style="text-align: center; color: #fff; padding: 10px 0">
          <router-link to="/home" class="homecolor"> 回到首页 </router-link>
        </li>
        <li>
          <p class="line"></p>
          <ul class="child-list clearfix">
            <li><a href="#">公告</a></li>
            <router-link tag="li" class="shopping" to="/personal"
              ><a>我的订单</a></router-link
            >
          </ul>
        </li>
        <li>
          <!-- 购物车 -->
          <router-link class="shopping" to="/shoppingcar"></router-link>
          <span class="num" v-if="ShopcarLists">{{ShopcarLists.length}}</span>
          <!-- <span class="num">0</span> -->
        </li>
        <li>
          <p class="ai"></p>
          <ul class="child-list clearfix">
            <router-link tag="li" class="shopping" to="/personal"
              ><a>我的收藏</a></router-link
            >
          </ul>
        </li>
        <li>
          <p class="other"></p>
          <ul class="child-list clearfix">
            <li><a href="#">客服热线：4001-123456</a></li>
          </ul>
        </li>
        <img @click="backTop" src="../assets/img/side_top.png" alt="" />
      </ul>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 回到顶部
      btnFlag: false,
      ShopcarLists:[]
    };
  },
  methods: {
    // 点击图片回到顶部方法，加计时器是为了过渡顺滑
    backTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    },
    // 为了计算距离顶部的高度，当高度大于60显示回顶部图标，小于60则隐藏
    scrollToTop() {
      let that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 200) {
        that.btnFlag = true;
      } else {
        that.btnFlag = false;
      }
    },
  },
  created() {
    // 购物车
    if (localStorage.Shopcar) {
      this.ShopcarLists = JSON.parse(localStorage.getItem("Shopcar"));
    }
  },
  //进入页面后启动定时轮询
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  },
};
</script>

<style lang="scss" scoped>
.sidebar {
  /* 侧边栏 */
  .rightbar {
    width: 40px;
    height: 100%;
    position: fixed;
    top: 0;
    right: 0;
    background-color: #b8a17b;
    z-index: 110;
  }

  .rightbar .sidebar_ul {
    top: 50%;
    margin-top: 250px;
    width: 40px;
    border-top: 1px solid #fff;
  }

  .rightbar .sidebar_ul > li {
    border-bottom: 1px solid #fff;
  }
  .homecolor {
    color: #fff;
  }
  .rightbar .sidebar_ul > li:hover {
    background-color: #dbd0bd;
    z-index: 999;
  }

  .rightbar .sidebar_ul > li:hover > ul {
    display: block;
  }

  .child-list {
    /* width: 250px; */
    height: 40px;
    margin-left: -270px;
    position: absolute;
    right: 40px;
    margin-top: -40px;
    background-color: #fff;
    display: none;
  }

  .child-list > li {
    float: left;
    line-height: 40px;
  }

  .child-list > li > a {
    margin: 0 10px;
  }

  .rightbar > ul > li:nth-child(1) {
    width: 100%;
    height: 100%;
  }

  .rightbar > ul > li:nth-child(2) .line {
    background: url("../assets/img/10052.png");
    cursor: pointer;
    z-index: 1;
  }

  .rightbar > ul > li:nth-child(3) {
    width: 100%;
    height: 80px;
    text-align: center;
  }

  .rightbar > ul > li:nth-child(3) .shopping {
    display: block;
    background: url("../assets/img/10053.png");
    width: 100%;
    height: 50%;
  }

  .rightbar > ul > li:nth-child(3) .num {
    line-height: 40px;
    color: #fff;
  }

  .rightbar > ul > li {
    width: 100%;
    height: 40px;
  }

  .rightbar > ul > li p {
    width: 100%;
    height: 40px;
  }

  .rightbar > ul > li:nth-child(4) .ai {
    display: block;
    background: url("../assets/img/10054.png");
  }

  .rightbar > ul > li:nth-child(5) .other {
    display: block;
    background: url("../assets/img/10056.png");
  }
}

/* 侧边栏结束 */
</style>